<template>
  <div class="image-to-image-page">
    <div class="page-header">
      <h1 class="page-title">图生图</h1>
      <p class="page-description">上传一张参考图片，让AI为您生成相似风格或内容的新图片</p>
    </div>

    <div class="main-content">
      <!-- 左侧控制面板 -->
      <div class="control-panel">
        <div class="control-section">
          <h3 class="section-title">上传参考图片</h3>
          <div class="upload-area" @click="triggerUpload">
            <input 
              ref="fileInput" 
              type="file" 
              accept="image/*" 
              @change="handleImageUpload"
              style="display: none"
            />
            <div v-if="!referenceImage" class="upload-placeholder">
              <div class="upload-icon">📷</div>
              <p>点击上传参考图片</p>
              <span class="upload-hint">支持 JPG、PNG 格式</span>
            </div>
            <div v-else class="uploaded-image">
              <img :src="referenceImage" alt="参考图片" class="reference-img" />
              <button class="remove-btn" @click.stop="removeImage">❌</button>
            </div>
          </div>
        </div>

        <div class="control-section">
          <h3 class="section-title">描述文字 (可选)</h3>
          <div class="prompt-area">
            <textarea
              v-model="prompt"
              :disabled="isGenerating"
              placeholder="补充描述您希望生成的图片特点，例如：改变背景为夕阳西下的海边..."
              class="prompt-textarea"
              rows="4"
            ></textarea>
          </div>
        </div>

        <div class="control-section">
          <h3 class="section-title">生成设置</h3>
          <div class="settings-group">
            <div class="setting-item">
              <label class="setting-label">生成模式</label>
              <div class="mode-options">
                <button 
                  v-for="mode in generationModes" 
                  :key="mode.id"
                  :class="['mode-btn', { active: selectedMode === mode.id }]"
                  @click="selectMode(mode.id)"
                >
                  <span class="mode-icon">{{ mode.icon }}</span>
                  <span class="mode-name">{{ mode.name }}</span>
                </button>
              </div>
            </div>

            <div class="setting-item">
              <label class="setting-label">相似度</label>
              <div class="slider-group">
                <input 
                  v-model="similarity" 
                  type="range" 
                  min="0" 
                  max="100" 
                  class="setting-slider"
                />
                <span class="slider-value">{{ similarity }}%</span>
              </div>
            </div>

            <div class="setting-item">
              <label class="setting-label">创新度</label>
              <div class="slider-group">
                <input 
                  v-model="innovation" 
                  type="range" 
                  min="0" 
                  max="100" 
                  class="setting-slider"
                />
                <span class="slider-value">{{ innovation }}%</span>
              </div>
            </div>
          </div>
        </div>

        <div class="control-section">
          <div class="action-controls">
            <button 
              class="action-btn primary large" 
              @click="generateImage" 
              :disabled="!referenceImage || isGenerating"
            >
              {{ isGenerating ? '生成中...' : '开始生成' }}
            </button>
            <button class="action-btn" @click="clearAll" :disabled="!referenceImage && generatedImages.length === 0">
              清空重置
            </button>
            <button class="action-btn" @click="downloadAll" :disabled="generatedImages.length === 0">
              批量下载
            </button>
          </div>
        </div>
      </div>

      <!-- 右侧预览区域 -->
      <div class="preview-area">
        <div class="result-container">
          <h3 class="result-title">生成结果</h3>
          
          <div v-if="!isGenerating && generatedImages.length === 0" class="empty-state">
            <div class="empty-icon">🎨</div>
            <p>上传参考图片，开始AI图片生成</p>
          </div>

          <div v-if="isGenerating" class="generating-state">
            <div class="generating-animation">
              <div class="camera-icon">📸</div>
              <div class="arrow">→</div>
              <div class="magic-icon">✨</div>
            </div>
            <p class="generating-text">AI正在基于您的图片生成新作品...</p>
            <div class="progress-bar">
              <div class="progress-fill" :style="{ width: progress + '%' }"></div>
            </div>
            <span class="progress-text">{{ progress }}%</span>
          </div>

          <div v-if="generatedImages.length > 0" class="images-grid">
            <div 
              v-for="(image, index) in generatedImages" 
              :key="index"
              class="image-card"
            >
              <div class="image-wrapper">
                <img :src="image" :alt="`生成图片 ${index + 1}`" class="result-image" />
                <div class="image-overlay">
                  <button class="overlay-btn" @click="downloadImage(image, index)">
                    📥 下载
                  </button>
                  <button class="overlay-btn" @click="useAsReference(image)">
                    🔄 作为参考
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ImageToImage',
  data() {
    return {
      referenceImage: null,
      prompt: '',
      isGenerating: false,
      progress: 0,
      generatedImages: [],
      selectedMode: 'style_transfer',
      similarity: 70,
      innovation: 60,
      
      generationModes: [
        { id: 'style_transfer', name: '风格迁移', icon: '🎭' },
        { id: 'content_variation', name: '内容变化', icon: '🔀' },
        { id: 'enhancement', name: '图片增强', icon: '✨' },
        { id: 'artistic', name: '艺术化', icon: '🎨' }
      ]
    }
  },
  methods: {
    triggerUpload() {
      this.$refs.fileInput.click();
    },
    
    handleImageUpload(event) {
      const file = event.target.files[0];
      if (file) {
        const reader = new FileReader();
        reader.onload = (e) => {
          this.referenceImage = e.target.result;
          this.generatedImages = []; // 清空之前的结果
        };
        reader.readAsDataURL(file);
      }
    },
    
    removeImage() {
      this.referenceImage = null;
      this.generatedImages = [];
      this.$refs.fileInput.value = '';
    },
    
    selectMode(modeId) {
      this.selectedMode = modeId;
    },
    
    async generateImage() {
      if (!this.referenceImage) return;
      
      this.isGenerating = true;
      this.progress = 0;
      this.generatedImages = [];
      
      // 模拟AI生成进度
      const progressInterval = setInterval(() => {
        this.progress += Math.random() * 12;
        if (this.progress >= 88) {
          this.progress = 88;
          clearInterval(progressInterval);
        }
      }, 350);
      
      // 模拟AI处理过程
      setTimeout(() => {
        clearInterval(progressInterval);
        this.progress = 100;
        
        // 生成模拟图片
        this.generatedImages = [
          'https://via.placeholder.com/512x512/FF9F43/FFFFFF?text=AI变换图片1',
          'https://via.placeholder.com/512x512/00D2D3/FFFFFF?text=AI变换图片2',
          'https://via.placeholder.com/512x512/FF6B6B/FFFFFF?text=AI变换图片3',
          'https://via.placeholder.com/512x512/4834D4/FFFFFF?text=AI变换图片4'
        ];
        
        this.isGenerating = false;
      }, 4000);
    },
    
    clearAll() {
      this.referenceImage = null;
      this.generatedImages = [];
      this.prompt = '';
      this.progress = 0;
      this.$refs.fileInput.value = '';
    },
    
    downloadImage(imageUrl, index) {
      const link = document.createElement('a');
      link.download = `image_to_image_${index + 1}.png`;
      link.href = imageUrl;
      link.click();
    },
    
    downloadAll() {
      this.generatedImages.forEach((image, index) => {
        setTimeout(() => {
          this.downloadImage(image, index);
        }, index * 200);
      });
    },
    
    useAsReference(imageUrl) {
      this.referenceImage = imageUrl;
      this.generatedImages = [];
    }
  }
}
</script>

<style scoped>
.image-to-image-page {
  padding: 1.5rem;
  min-height: 100vh;
  background: linear-gradient(135deg, #ff9f43 0%, #ff6b6b 100%);
}

.page-header {
  text-align: center;
  margin-bottom: 2rem;
}

.page-title {
  font-size: 2.5rem;
  font-weight: bold;
  color: white;
  margin-bottom: 1rem;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.page-description {
  font-size: 1.1rem;
  color: rgba(255, 255, 255, 0.9);
  max-width: 600px;
  margin: 0 auto;
}

.main-content {
  display: flex;
  gap: 2rem;
  max-width: 1400px;
  margin: 0 auto;
}

.control-panel {
  width: 350px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  height: fit-content;
}

.control-section {
  margin-bottom: 2rem;
}

.section-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: #2d3748;
  margin-bottom: 1rem;
}

.upload-area {
  border: 2px dashed #ff9f43;
  border-radius: 12px;
  padding: 1.5rem;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #fff5e6;
  position: relative;
}

.upload-area:hover {
  border-color: #ff6b6b;
  background-color: #ffe8e6;
}

.upload-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.upload-icon {
  font-size: 2.5rem;
  margin-bottom: 0.5rem;
}

.upload-hint {
  font-size: 0.8rem;
  color: #a0aec0;
}

.uploaded-image {
  position: relative;
  display: flex;
  justify-content: center;
}

.reference-img {
  max-width: 100%;
  max-height: 200px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.remove-btn {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: none;
  background: #ff4757;
  color: white;
  font-size: 0.8rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.prompt-textarea {
  width: 100%;
  padding: 1rem;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 0.95rem;
  line-height: 1.6;
  resize: vertical;
  transition: all 0.3s ease;
  background: white;
}

.prompt-textarea:focus {
  outline: none;
  border-color: #ff9f43;
  box-shadow: 0 0 0 3px rgba(255, 159, 67, 0.1);
}

.settings-group {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.setting-item {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.setting-label {
  font-size: 0.9rem;
  font-weight: 500;
  color: #4a5568;
}

.mode-options {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

.mode-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  gap: 0.25rem;
}

.mode-btn:hover {
  border-color: #ff9f43;
  background-color: #fff5e6;
}

.mode-btn.active {
  border-color: #ff9f43;
  background-color: #ffe8e6;
  color: #e55039;
}

.mode-icon {
  font-size: 1.25rem;
}

.mode-name {
  font-size: 0.8rem;
  font-weight: 500;
}

.slider-group {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.setting-slider {
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: #e2e8f0;
  outline: none;
}

.slider-value {
  font-size: 0.875rem;
  font-weight: 600;
  color: #ff9f43;
  min-width: 45px;
}

.action-controls {
  display: grid;
  gap: 0.75rem;
}

.action-btn {
  padding: 0.875rem 1rem;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-btn.primary {
  background: linear-gradient(135deg, #ff9f43 0%, #ff6b6b 100%);
  color: white;
}

.action-btn.primary.large {
  padding: 1rem 1.5rem;
  font-size: 1.1rem;
}

.action-btn.primary:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(255, 159, 67, 0.4);
}

.action-btn:not(.primary) {
  background: rgba(255, 255, 255, 0.9);
  color: #4a5568;
  border: 1px solid #e2e8f0;
}

.action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.preview-area {
  flex: 1;
}

.result-container {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 2rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.result-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: #2d3748;
  margin-bottom: 1.5rem;
  text-align: center;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400px;
  color: #a0aec0;
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 1rem;
}

.generating-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400px;
  gap: 1.5rem;
}

.generating-animation {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.camera-icon, .magic-icon {
  font-size: 3rem;
  animation: pulse 2s ease-in-out infinite;
}

.arrow {
  font-size: 2rem;
  color: #ff9f43;
  animation: slide 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

@keyframes slide {
  0%, 100% { transform: translateX(0); }
  50% { transform: translateX(10px); }
}

.generating-text {
  font-weight: 600;
  color: #2d3748;
}

.progress-bar {
  width: 300px;
  height: 8px;
  background: #e2e8f0;
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #ff9f43, #ff6b6b);
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 0.9rem;
  font-weight: 600;
  color: #ff9f43;
}

.images-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 1.5rem;
}

.image-card {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.image-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}

.image-wrapper {
  position: relative;
}

.result-image {
  width: 100%;
  height: 250px;
  object-fit: cover;
  display: block;
}

.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.image-card:hover .image-overlay {
  opacity: 1;
}

.overlay-btn {
  padding: 0.5rem 1rem;
  background: rgba(255, 255, 255, 0.9);
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  transition: all 0.3s ease;
}

.overlay-btn:hover {
  background: white;
  transform: scale(1.05);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .main-content {
    flex-direction: column;
  }
  
  .control-panel {
    width: 100%;
  }
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }
  
  .mode-options {
    grid-template-columns: 1fr;
  }
  
  .images-grid {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  }
}
</style> 
